package com.suse.ecommerce.serviceImp;

import com.suse.ecommerce.dao.ShoppingMapper;
import com.suse.ecommerce.pojo.Shopping;
import com.suse.ecommerce.pojo.ShoppingExample;
import com.suse.ecommerce.service.ShoppingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ShoppingServiceImpl implements ShoppingService {
    @Autowired
    private ShoppingMapper shoppingMapper;
    public void updateShoppingNum(long shoppingId,Integer num){
        //如果num等于0，则直接删除，
        if(num==0){
            shoppingMapper.deleteByPrimaryKey(shoppingId);
        }else{
            Shopping shopping=new Shopping();
            shopping.setNum(num);
            shopping.setShoppingId(shoppingId);
            shoppingMapper.updateByPrimaryKeySelective(shopping);
        }
    }


    /**
     *根据userId查询对应用户的购物车商品信息
     */
    @Override
    public List<Shopping> queryShopping(Integer userId) {
        ShoppingExample example=new ShoppingExample();
        ShoppingExample.Criteria criteria = example.createCriteria();
        criteria.andUserIdEqualTo(userId);
        List<Shopping> shoppings=shoppingMapper.selectByExampleWithBLOBs(example);
        return shoppings;
    }

    @Override
    public long queryShoppingByPidAndUid(long product, Integer userId) {
        List<Long> shoppings = shoppingMapper.queryShoppingByPidAndUid(userId, product);
        if(shoppings.size()==0){
            return 0;
        }else{
            return shoppings.get(0);
        }

    }

    @Override
    public void addShopping(Shopping shopping) {
        shoppingMapper.insertSelective(shopping);
    }

    @Override
    public void updateShoppingNumAdd(long shoppngId) {
        shoppingMapper.updateShoppingNumAdd(shoppngId);
    }

    @Override
    public void delShopping(Shopping shopping) {
        shoppingMapper.delShopping(shopping);
    }
}
